package com.baozhun.mall.common.adapter

import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.BR
import com.baozhun.mall.common.R
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseListAdapter
import com.baozhun.mall.common.databinding.ItemClassifyLayoutBinding
import com.baozhun.mall.common.model.bean.ActivityInfoBean
import com.baozhun.mall.common.model.bean.ClassifyInfo
import com.baozhun.mall.common.util.Constants
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/12/19
 * @Version：1.0.0
 */
class ClassifyListAdapter(data: MutableList<ClassifyInfo>?) :
    BaseListAdapter<ClassifyInfo, ItemClassifyLayoutBinding>(R.layout.item_classify_layout, data) {

    override fun convert(holder: BaseDataBindingHolder<ItemClassifyLayoutBinding>, item: ClassifyInfo) {
        holder.dataBinding?.setVariable(BR.model, item.name)
        if (item.children?.isNotEmpty() == true) {
            val adapter = SubClassifyListAdapter(item.children)
            holder.dataBinding?.subClassifyRv?.adapter = adapter
        }
        holder.dataBinding?.subClassifyRv?.visibility = if (item.isShown) View.VISIBLE else View.GONE
        holder.dataBinding?.classifyTv?.setOnClickListener {
             if (item.children.isEmpty()) {
                 ARouter.getInstance().build(RoutePath.Home.ACTIVITY_DETAIL)
                     .withSerializable(Constants.Common.BUNDLE_NAME, ActivityInfoBean(item.id,  Constants.Common.TYPE_CLASSIFY))
                     .withString(Constants.Common.TITLE, item.name)
                     .navigation()
             } else {
                 item.isShown = !item.isShown
                 holder.dataBinding?.subClassifyRv?.visibility = if (item.isShown) View.VISIBLE else View.GONE
                 holder.dataBinding?.classifyTv?.setBackgroundColor(if (item.isShown) context.resources.getColor(com.baozhun.mall.res.R.color.color_F8F8F8)
                 else context.resources.getColor(com.baozhun.mall.res.R.color.white))
                 holder.dataBinding?.classifyIv?.setImageResource(if (item.isShown) com.baozhun.mall.res.R.drawable.ic_arrow_up else R.drawable.ic_arrow_down)
             }
        }
    }
}